﻿using MaterialDesignThemes.Wpf;

using PLCS.App.Shared.Events;
using PLCS.App.Shared.Models;
using PLCS.Domain.Shared.Enums;

namespace PLCS.App.Shared.ViewModels.UserControls.Chlidren
{
    public class EditConveyorLineControlDialogViewModel : HostDialogViewModel
    {
        private ModelControl _model;
        private readonly IEventAggregator _eventAggregator;

        public ModelControl Model
        {
            get { return _model; }
            set { SetProperty(ref _model, value); }
        }

        protected override void Save()
        {
            Model.EquipmentOrStation = "Station";
            if (!string.IsNullOrEmpty(Model.Plc_Index) && !string.IsNullOrEmpty(Model.PLCAddress))
            {
                Model.EquipmentOrStation = "Equipment";
                Model.ContainStationNos = Model.SName;
                Model.EquipmentType = ((int)Enum.Parse(typeof(EquipmentType), "ConveyorLine")).ToString();
            }
            _eventAggregator.GetEvent<ModelControlEvent>().Publish(Model);
            DialogHost.Close(IdentifierName, new DialogResult(ButtonResult.OK));
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            var value = parameters.GetValue<ModelControl>("model");
            value ??= new ModelControl()
            {
                Capacity = "1",
                StationType = "普通工位",
                MultipleTrayType = "1",
                ReadPLCNo = "ET1"
            };
            value.ControlType = "ConveyorLine";
            Model = value;
        }

        public EditConveyorLineControlDialogViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
        }
    }
}